﻿/*!
 * lhgcore Dialog Plugin v3.0.2
 * Date : 2010-04-21 09:03:11
 * Copyright (c) 2009 - 2010 By Li Hui Gang
 */

;(function(J){

var top = window, cover, doc, dragDiv, ZIndex;

while( top.parent != top )
    top = top.parent;
	
doc = top.document;

function getAbsoultePath()
{
	var sc = J('script'), bp = '', i = 0, l = sc.length;
	
	for( ; i < l; i++ )
	{
	    if( sc[i].src.indexOf('lhgdialog.js') >= 0 )
		{
		    bp = !!document.querySelector ?
			    sc[i].src : sc[i].getAttribute('src',4);
			break;
		}
	}
	
	return bp.substr( 0, bp.lastIndexOf('/') + 1 );
};

function getsrc()
{
	if( J.browser.ie )
		return ( J.browser.i7 ? '' : "javascript:''" );
	else
		return 'javascript:void(0);';
};

function getZIndex()
{
    if( !ZIndex ) ZIndex = 999;
	
	return ++ZIndex;
};

function reSizeHdl()
{
    var rel = J.root( doc );
	
	J(cover).css({
	    width: Math.max( rel.scrollWidth, rel.clientWidth || 0 ) - 1 + 'px',
		height: Math.max( rel.scrollHeight, rel.clientHeight || 0 ) - 1 + 'px'
	});
};

function getScrollSize( $ )
{
    $ = $ || window;
	if( 'pageXOffset' in $ )
	{
	    return {
		    x: $.pageXOffset || 0,
			y: $.pageYOffset || 0
		};
	}
	else
	{
	    var doc = J.root( $.document );
		return {
		    x: doc.scrollLeft || 0,
			y: doc.scrollTop || 0
		};
	}
};

function getClientSize( $ )
{
    $ = $ || window;
	var doc = J.root( $.document );
	return {
	    w: doc.clientWidth || 0,
		h: doc.clientHeight || 0
	}
};

function cleanDiv()
{
    J(dragDiv).remove();
	dragDiv = null;
	
	if( cover )
	{
	    J(cover).remove();
		cover = null;
	}
};

J.fn.dialog = function( opts )
{
    var dialog = false;
	
	if( this[0] )
	    dialog = new J.dialog( opts, this[0] );
	
	return dialog;
};

J.dialog = function( opts, elem )
{
    var r = this.opt =
	J.extend({
	    height: 300,
		width: 400,
		id: 'lhgdlgId',
		event: 'click',
		link: false,
		btns: true,
		drag: true,
		resize: true,
		title: 'lhgdialog',
		regDragWindow: []
	}, opts || {} );
	
	if( r.SetTopWindow )
	{
	    top = r.SetTopWindow;
		doc = top.document;
	}
	
	if( !J('#lhgdlgcss',doc)[0] )
	{
	    J('head',doc).prepend(
		    '<link type="text/css" href="' + getAbsoultePath() + 
			'lhgdialog.css" id="lhgdlgcss" rel="stylesheet"\/>'
		);
	}
	
	var self = this, innerDoc = '', text,
	
	iframe = J.browser.ie && !J.browser.i7 ? '<iframe hideFocus="true" ' + 
	    'frameborder="0" src="' + getsrc() + '" style="position:absolute;' +
		'z-index:-1;width:100%;height:100%;top:0px;left:0px;filter:' +
		'progid:DXImageTransform.Microsoft.Alpha(opacity=0)"><\/iframe>' : '';
	
	if( r.html && (typeof r.html === 'string') )
	    innerDoc = r.html;
	else if( r.page )
	{
	    innerDoc = ['<iframe width="100%" height="100%" id="lhgfrm" src="',
		    r.page, '" frameborder="0" scrolling="auto"><\/iframe>'].join('');
	}
	
	text = ['<div id="', r.id, '" class="lhgdig" style="width:', r.width, 
		'px;height:', r.height, 'px;"><div id="top"><span class="t_m" ',
		'id="drag"><em id="txt">', r.title, '</em></span><div id="xbtn" ',
		'class="xbtn"></div></div><span class="t_l"></span><span class="t_r">',
		'</span><div id="middle"><span class="m_l" id="m_l"></span><span ',
		'class="m_r"></span><div id="context"><div id="inbox">', innerDoc,
		'</div>', r.btns ? '<div id="btns"></div>' : '', '</div></div>',
		'<div id="foot"><div class="b_m"></div></div><span class="b_l"></span>',
		'<span class="b_r" id="drop"></span>', iframe, '<div id="throbber">',
		'<span>loading...</span></div></div>' ].join('');
	
	if( !dragDiv )
	{
		dragDiv = J('<div id="cDiv" style="position:absolute;top:0px;left:0px;' +
		    'border:1px solid #000;background-color:#999;display:none;"></div>',doc)
			.css('opacity',0.3).appendTo('body')
			.bind( 'contextmenu', function(ev){ ev.preventDefault(); })[0];
	}
	
	this.ShowDialog = function()
	{
	    if( J('#'+r.id,doc)[0] )
		    return;
		
		if( r.cover )
		    this.ShowCover();
			
		var sn = getClientSize(top), sl = getScrollSize(top),
		
		iTop = r.top ? r.top :
		    Math.max( sl.y + ( sn.h - r.height - 20 ) / 2, 0 ),
		iLeft = r.left ? r.left :
			Math.max( sl.x + ( sn.w - r.width - 20 ) / 2, 0 );
		
		this.dlg = J(text,doc).css({
		    top: iTop + 'px', left: iLeft + 'px', zIndex: getZIndex()
		}).appendTo(doc.body)[0];
		
		this.reSizeContent();
		this.setDialog( this.dlg );
		
		if( r.html && r.cusfn ) r.cusfn();
	
	    if( r.drag )
		    this.initDrag( J('#drag',this.dlg)[0] );
		
		if( r.resize )
		    this.initSize( J('#drop',this.dlg)[0] );
		
		if( r.link || r.html )
		    J('#throbber',this.dlg).css('display','none');
	};
	
	this.ShowCover = function()
	{
	    if( !cover )
		{
			var html = [ '<div style="position:absolute;top:0px;left:0px;',
					'background-color:#fff;">', iframe, '</div>' ].join('');
			
			cover = J(html,doc).css('opacity',0.5).appendTo(doc.body)[0];
		}
		
		J(top).bind( 'resize', reSizeHdl );
		reSizeHdl();
		J(cover).css({ display: '', zIndex: getZIndex() });
	};
	
	this.cancel = function()
	{
		var frm = J('#lhgfrm',self.dlg)[0];
		if( frm )
		{
			if( !self.opt.link )
				J(frm.contentWindow).unbind( 'load' );
			frm.src = getsrc(); frm = null;
		}
		
		self.regWindow = [];
		J(self.dlg).remove(); self.dlg = null;
		
		if( cover )
		{
		    if( self.opt.parent && self.opt.parent.opt.cover )
			{
			    var Index = self.opt.parent.dlg.style.zIndex;
				cover.style.zIndex = parseInt(Index,10) - 1;
			}
			else
			    cover.style.display = 'none';
		}
	};
	
	this.reload = function( win, url )
	{
	    win = win || window;
		self.cancel();
		
		win.location.href = url ? url : win.location.href;
	};
	
	this.reSizeContent = function( height )
	{
	    var dlg = this.dlg, iHeight,
		dh = height ? height : dlg.offsetHeight,
		th = J('#top',dlg)[0].offsetHeight,
		bh = J('#foot',dlg)[0].offsetHeight;
		
		iHeight = dh - th - bh;
		
		J('#middle,#throbber',dlg).css( 'height', iHeight + 'px' );
		
		J('#context,#throbber',dlg).css( 'width',
		    dlg.offsetWidth - J('#m_l',dlg)[0].offsetWidth * 2 + 'px'
		);
		
		J('#inbox',dlg).css( 'height', iHeight - 
		    (r.btns ? J('#btns',dlg)[0].offsetHeight : 0) + 'px'
		);
		
		if( J.browser.ie && doc.compatMode !== 'CSS1Compat' )
		    J('#context',dlg).css('top','0px');
		
		if( J.browser.i8 && r.page )
		    J('#lhgfrm',dlg).css( 'height', J('#inbox',dlg)[0].style.height );
	};
	
	this.reDialogSize = function( width, height )
	{
		J(this.dlg).css({
		    'width': width + 'px', 'height': height + 'px'
		});
		
		this.reSizeContent( J.browser.ie && !J.browser.i7 ? height : '' );
	};
	
	this.setDialog = function( dlg )
	{
		this.win = window;
		this.top = top;
		
		J(dlg).bind('contextmenu',function(ev){
		    ev.preventDefault();
		}).bind( 'mousedown', self.setIndex );
		
		J('#xbtn',dlg).hover(function(){
		    J(this).addClass('xbtnover');
		},function(){
		    J(this).removeClass('xbtnover');
		}).click( self.cancel );
		
		if( r.html && r.html.nodeType )
		    J('#inbox',dlg).html( r.html );
		
		this.regWindow = [ window ];
		
		if( r.regDragWindow.length > 0 )
		    this.regWindow.push( r.regDragWindow );
		
		if( top != window )
		    this.regWindow.push( top );
		
	    if( r.page && !r.link )
		{
		    this.inwin = J('#lhgfrm',dlg)[0].contentWindow;
			this.inwin.dg = this;
			this.regWindow.push( this.inwin );
			
			var indoc = this.inwin.document;
			J( J.browser.ie ? indoc : this.inwin )
			.bind( 'mousedown', self.setIndex );
			
			J( this.inwin ).bind( 'load', function(){
			    self.indoc = self.inwin.document;
				J('#throbber',self.dlg).css('display','none');
			});
		}
	};
	
	this.setIndex = function(ev)
	{
		self.dlg.style.zIndex = parseInt(ZIndex,10) + 1;
		ZIndex = parseInt( self.dlg.style.zIndex, 10 );
		
		ev.stopPropagation();
	};
	
	this.addBtn = function( id, txt, fn )
	{
	    if( J('#'+id,this.dlg)[0] )
		    J('#'+id,this.dlg).html( txt ).click( fn );
		else
		{
			J('<div class="button" id="' + id + '">' + txt + '</div>',doc)
			.click(fn).hover(function(){
			    J(this).addClass('btnover');
			},function(){
			    J(this).removeClass('btnover');
			}).appendTo('#btns');
		}
	};
	
	this.removeBtn = function( id )
	{
	    var btn = J('#'+id,this.dlg)[0];
		
		if( btn )
		{
		    J(btn).remove(); btn = null;
		}
	};
	
	this.initDrag = function( elem )
	{
	    var lacoor, dH, dW, curpos, regw = this.regWindow,
		    ciS = getClientSize(top), scS = getScrollSize(top);
		
		function moveHandler(ev)
		{
			var curcoor = { x: ev.screenX, y: ev.screenY };
		    curpos =
		    {
		        x: curpos.x + ( curcoor.x - lacoor.x ),
			    y: curpos.y + ( curcoor.y - lacoor.y )
		    };
			lacoor = curcoor;
			
			if( r.rang )
			{
			    if( curpos.x < scS.x ) curpos.x = scS.x;
				if( curpos.y < scS.y ) curpos.y = scS.y;
				
				if( curpos.x + dW > ciS.w + scS.x )
				    curpos.x = ciS.w + scS.x - dW;
				if( curpos.y + dH > ciS.h + scS.y )
				    curpos.y = ciS.h + scS.y - dH;
			}
			
			J(dragDiv).css({ left: curpos.x + 'px', top: curpos.y + 'px' });
		};
		
		function upHandler(ev)
		{
			for( var i = 0, l = regw.length; i < l; i++ )
			{
			    J( regw[i].document ).unbind( 'mousemove', moveHandler );
				J( regw[i].document ).unbind( 'mouseup', upHandler );
			}
			
		    if( J.browser.ie )
			    dragDiv.releaseCapture();
			
			dragDiv.style.display = 'none'; lacoor = null; elem = null;
			J(self.dlg).css({ left: curpos.x + 'px', top: curpos.y + 'px' });
		};
		
		J(elem).bind( 'mousedown', function(ev){
		    if( ev.target.id === 'xbtn' ) return;
			
			dW = self.dlg.offsetWidth;
			dH = self.dlg.offsetHeight;
			
			curpos = { x: self.dlg.offsetLeft, y: self.dlg.offsetTop };
			lacoor = { x: ev.screenX, y: ev.screenY };
			
			J(dragDiv).css({
			    width: dW + 'px', height: dH + 'px', left: curpos.x + 'px',
				top: curpos.y + 'px', zIndex: parseInt(ZIndex,10) + 2, display: ''
			});
			
			for( var i = 0, l = regw.length; i < l; i++ )
			{
				J( regw[i].document ).bind( 'mousemove', moveHandler );
				J( regw[i].document ).bind( 'mouseup', upHandler );
			}
			
			ev.preventDefault();
			
			if( J.browser.ie ) dragDiv.setCapture();
		});
	};
	
	this.initSize = function( elem )
	{
	    var lacoor, dH, dW, curpos, regw = this.regWindow, dialog,
		    ciS = getClientSize(top), scS = getScrollSize(top);
		
		function moveHandler(ev)
		{
		    var curcoor = { x : ev.screenX, y : ev.screenY };
			dialog = {
				w: curcoor.x - lacoor.x,
				h: curcoor.y - lacoor.y
			};
			
			if( dialog.w < 200 )
			    dialog.w = 200;
			if( dialog.h < 100 )
			    dialog.h = 100;
			
			J(dragDiv).css({
			    width: dialog.w + 'px', height: dialog.h + 'px',
				top: curpos.y + 'px', left: curpos.x + 'px'
			});
		};
		
		function upHandler(ev)
		{
			for( var i = 0, l = regw.length; i < l; i++ )
			{
			    J( regw[i].document ).unbind( 'mousemove', moveHandler );
				J( regw[i].document ).unbind( 'mouseup', upHandler );
			}
			
		    if( J.browser.ie )
			    dragDiv.releaseCapture();
			
			self.reDialogSize( dialog.w, dialog.h );
			dragDiv.style.display = 'none'; lacoor = null; elem = null;
		};
	
	    J(elem).bind( 'mousedown', function(ev){
			dW = self.dlg.offsetWidth;
			dH = self.dlg.offsetHeight;
			
			curpos = { x: self.dlg.offsetLeft, y: self.dlg.offsetTop };
			lacoor = { x: ev.screenX - dW, y: ev.screenY - dH };
			
			J(dragDiv).css({
			    width: dW + 'px', height: dH + 'px', left: curpos.x + 'px',
				top: curpos.y + 'px', zIndex: parseInt(ZIndex,10) + 2, display: ''
			});
			
			for( var i = 0, l = regw.length; i < l; i++ )
			{
			    J( regw[i].document ).bind( 'mousemove', moveHandler );
				J( regw[i].document ).bind( 'mouseup', upHandler );
			}
			
			ev.preventDefault();
			
			if( J.browser.ie ) dragDiv.setCapture();
		});
	};
	
	this.cleanDialog = function()
	{
		if( self.dlg )
		{
			var frm = J('#lhgfrm',self.dlg)[0];
			if( frm )
			{
				if( !self.opt.link )
				    J(frm.contentWindow).unbind( 'load' );
				frm.src = getsrc(); frm = null;
			}
			
			J(self.dlg).remove(); self.dlg = null;
		}
	};
	
	J(window).bind( 'unload', this.cleanDialog );
	
	if( elem )
	    J(elem).bind( r.event, function(){ self.ShowDialog(); });
};

J(window).bind( 'unload', cleanDiv );

})(lhgcore);